/**
 * Created by lvzj on 15/12/3.
 */

//为了解决ios我也要
if (window.name != "first") {
    location.reload();
    window.name = "first";
}
else {
    window.name = "";
}

$('.contentEmpty').hide();

var topicId = params.tid;
var token = params.token;
var deviceId = params.deviceId;
var title = "";
//var isMore =true;//判断是否有更多数据
var isfinish = true;//isfinish判断每次数据是否加载完成，完成了才可进行下一次加载
var type = "all";
var allTs = 0;
var aroundTs = 0;

function init() {
}
var topicImage,bigImg;
if (window.name == "") {
    $.get(serverUrl + '/rest/v1.0/Questions/getTopic/' + topicId + '/' + token + '/' + deviceId, function (json) {
        if (json.data.shareKey != null)
            setShareContent(json.data.shareKey);
        topicImage = json.data.topicImage;
        bigImg = json.data.bigImg;
        title = json.data.title;
        $('.title').html('#' + json.data.title + '#');
        $('.sub').html(json.data.subTitle);
        $('.time').html(json.data.dateCreated.substring(0, json.data.dateCreated.indexOf('T')));
        $('.dec').html('<pre>' + json.data.desc + '</pre>');
        $('#browserCount').html(json.data.browserCount);
        $('#topicImage').attr('src', json.data.topicImage);
    }, 'json');
    getContentList(allTs, 'all');
    getContentList(aroundTs, 'around');
}

function getContentList(ts, type) {
    $.get(serverUrl + '/rest/v1.0/Questions/getShareByTopicId/' + ts + '/' + topicId + '/' + type + '/' + token + '/' + deviceId, function (json) {

        if (json.data == null)return;

        var str = "";
        if (type == 'all') {
            allTs = json.data.nextTs == null ? 1 : json.data.nextTs;
        } else {
            aroundTs = json.data.nextTs == null ? 1 : json.data.nextTs;
        }

        var list = json.data.list;

        for (var i = 0; i < list.length; i++) {
            var question = list[i];
            str += '<li class="sub-con">';
            str += '<img src="' + question.headImg + '" onclick="toPeronalPage(\'' + question.createdBy.objId + '\')" class="avatar">';
            str += '<div class="userinfo" onclick="toQuestionDetail(\'' + question.objId + '\', \'' + question.createdBy.objId + '\', \'' + question.category + '\')">';
            str += '<p>' + question.createdBy.nickName + '</p>';
            str += '<span class="time">' + ft(question.dateCreated) + '</span>&nbsp;<span class="address">' + question.community.communityName + '</span>';
            str += '</div>';
            str += '<p>';
            str += '<a href="javascript:toQuestionDetail(\'' + question.objId + '\', \'' + question.createdBy.objId + '\', \'' + question.category + '\');">' + (question.desc.length > 100 ? (question.desc.substring(0, 60) + "...") : question.desc) + '</a>';
            str += '<div class="clearfix pic-box">';
            var imageList = new Array();
            if (question.images != null) {

                question.images.forEach(function (item) {
                    imageList.push(item.bigImg);
                })

                for (var j = 0; j < question.images.length; j++) {
                    var imageUrl = question.images[j].imageURL;
                    str += '<img onclick="showImage(\'' + imageList + '\',\'' + j + '\')" src="' + imageUrl + '" >';
                }
            }
            str += '</div>';
            str += '</p>';
            var zanDesc = question.praiseCount == 0 ? '赞' : question.praiseCount;
            var pinglun = "";
            if (question.replies == null || question.replies.length == 0) {
                pinglun = "评论";
            } else {
                pinglun = question.replies.length;
            }

            if (question.isPraise == true) {
                str += '<div class="clearfix data"><a style="background-image:url(\'' + resUrl + 'images/topic/zan1.png\');color:#fc724d;" class="zan' + question.objId + '" href="javascript:zan(\'' + question.objId + '\',' + question.isPraise + ');">' + zanDesc + '</a><a href="javascript:share(\'' + question.community.communityName + '\',\'' + question.desc + '\',\'' + question.objId + '\');">分享</a><a href="javascript:toQuestionDetail(\'' + question.objId + '\', \'' + question.createdBy.objId + '\', \'' + question.category + '\');">' + pinglun + '</a></div>';
            } else {
                str += '<div class="clearfix data"><a class="zan' + question.objId + '" href="javascript:zan(\'' + question.objId + '\',' + question.isPraise + ');">' + zanDesc + '</a><a href="javascript:share(\'' + question.community.communityName + '\',\'' + question.desc + '\',\'' + question.objId + '\');">分享</a><a href="javascript:toQuestionDetail(\'' + question.objId + '\', \'' + question.createdBy.objId + '\', \'' + question.category + '\');">' + pinglun + '</a></div>';
            }

            str += '</li>';
        }

        if (type == 'all') {
            $('.topic-part2').eq(0).html($('.topic-part2').eq(0).html() + str);
            if ($('.topic-part2').eq(0).html() == "") {
                $('.contentEmpty').show();
            } else {
                $('.contentEmpty').hide();
            }
        } else {
            $('.topic-part2').eq(1).html($('.topic-part2').eq(1).html() + str);
        }

        isfinish = true;
    }, 'json');
}

function toSend() {
    if (params.vcode != null && Number(params.vcode) >= 301000) {
        if (params.type == 'android') {
            webviewhandle.sendTopic(topicId, title);
        } else {
            document.location.href = "66xiaoqu:send:topic##topicId=" + topicId + "&nbsp;title=" + title;
        }
    } else {
        location.href = htmlUrl + "/topic/send.html?isShare=false&type=" + params.type + "&topicId=" + topicId + "&token=" + params.token + "&deviceId=" + params.deviceId + "&title=" + escape(title);
    }
}

//按钮切换
$('.part2-nav a').each(function (i) {
    $('.topic-part2').eq(1).hide();
    $(this).click(function () {
        $(this).addClass('cur').siblings('a').removeClass('cur');
        $('.topic-part2').eq(i).show().siblings('.topic-part2').hide();
        if (type == 'all' && i != 0) {
            type = 'around';
            if ($('.topic-part2').eq(1).html() == "") {
                $('.contentEmpty').show();
            } else {
                $('.contentEmpty').hide();
            }
        } else if (type == 'around' && i != 1) {
            if ($('.topic-part2').eq(0).html() == "") {
                $('.contentEmpty').show();
            } else {
                $('.contentEmpty').hide();
            }
            type = "all";
        }
    });
});

//滑动加载数据
$('#wrapper').scroll(function () {
    var contentHeight = $("#wrapper")[0].scrollHeight;
    var h = $("#wrapper").height();
    if ((contentHeight - h - $(this).scrollTop()) < 60) {
        //$(".loading-tips").show().html('<img src="http://66xiaoqu.babytreeimg.com/images/topic/load.gif">加载中...');
        if (isfinish == true) {
            isfinish = false;
            if (type == 'all' && allTs != 1) {
                getContentList(allTs, 'all');
            } else if (type == 'around' && aroundTs != 1) {
                getContentList(aroundTs, 'around');
            } else {
                isfinish = true;
            }
        }
    }
})
;

function share(communityName, desc, id) {
    var url = serverUrl + "/pages/static/mobile/partials/share/web/index.html?feedsId=" + id;
    if (params.type == 'ios') {
        document.location.href = "66xiaoqu:market:share##title=" + communityName + "的帖子&nbsp;desc=" + desc + "&nbsp;image=" + resUrl + 'images/com/shareLogo.png' + "&nbsp;url=" + url;
    } else {
        webviewhandle.shareGoodsDetail(communityName + '的帖子', desc, resUrl + 'images/com/shareLogo.png', url);//webviewhandle为webview注册对象
    }
};

function zan(questionId, isPraise) {
    if (!isPraise) {
        $.get(serverUrl + '/rest/v1.0/Questions/praiseQuestion/' + questionId + '/' + params.token + '/' + params.deviceId, function (json) {

            $('.zan' + questionId).attr('href', 'javascript:zan(\'' + questionId + '\',true);');
            if (isNaN($('.zan' + questionId).html())) {
                $('.zan' + questionId).html(1)
            } else {
                $('.zan' + questionId).html(parseInt($('.zan' + questionId).html()) + 1);
            }
            $('.zan' + questionId).attr('style', 'background-image:url(\'' + resUrl + 'images/topic/zan1.png\');color:#fc724d;');
        });
    } else {
        $.get(serverUrl + '/rest/v1.0/Questions/cancelPraiseQuestion/' + questionId + '/' + params.token + '/' + params.deviceId, function (json) {
            $('.zan' + questionId).attr('href', 'javascript:zan(\'' + questionId + '\',false);');

            if (parseInt($('.zan' + questionId).html()) == 1) {
                $('.zan' + questionId).html('赞')
            } else {
                $('.zan' + questionId).html(parseInt($('.zan' + questionId).html()) - 1);
            }
            $('.zan' + questionId).attr('style', 'background-image:url(\'' + resUrl + 'images/topic/zan.png\');');
        });
    }
}

function toQuestionDetail(questionId, createdBy, category) {
    if (params.type == 'ios') {
        document.location.href = "66xiaoqu:toQuestionDetail##objId=" + questionId + "&nbsp;createdBy=" + createdBy;
        +"&nbsp;category=" + category;
    } else {
        webviewhandle.goToQuestionDetail(questionId, createdBy, category);//webviewhandle为webview注册对象
    }
};
function toPeronalPage(uid) {
    if (params.type == 'ios') {
        document.location.href = "66xiaoqu:toPeronalPage##userId=" + uid;
    } else {
        webviewhandle.goToPeronalPage(uid);//webviewhandle为webview注册对象
    }
};
function showImage(images, index) {
    var type = params.type;
    if (type == 'android') {
        webviewhandle.showImage(images, index);//webviewhandle为webview注册对象
    } else if (type == 'ios') {
        var vCode = params.vcode;
        if (vCode != null && parseInt(vCode) > 200033) {
            document.location.href = "66xiaoqu:showImage##images=" + images + "&nbsp;index=" + index;
        }
    }
}